// pages/personal/personal.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userProfileData: {},
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为
        canIUseGetUserProfile: true,
        headerImg: app.globalData.baseURL + '/img/avatar.png',
        isZh: app.globalData.isZh,
        baseURL: app.globalData.baseURL,
        pages: [{
            icon: app.globalData.baseURL + "/img/icon-instructions.png",
            label: app.globalData.isZh ? "操作说明书" : "Operating Instructions",
            target: "../instructions/instructions"
        }, {
            icon: app.globalData.baseURL + "/img/icon-contact.png",
            label: app.globalData.isZh ? "联系我们" : "Contact Us",
            target: "../contact/contact"
        }, {
            icon: app.globalData.baseURL + "/img/icon-polocy.png",
            label: app.globalData.isZh ? "隐私条款" : "Privacy Policy",
            target: "../polocy/polocy"
        },{
            icon: app.globalData.baseURL + "/img/icon-polocy.png",
            label: app.globalData.isZh ? "切换体检人" : "Privacy Policy",
            target: "../binding/binding"
        }]
    },
    onLoad() {
        if (!app.globalData.isZh) {
            wx.setTabBarItem({ index: 0, text: 'Measure' })
            wx.setTabBarItem({ index: 1, text: 'My' })
        }
        if (wx.getStorageSync('userProfileData')) {
            this.setData({
                hasUserInfo: true,
                userInfo: wx.getStorageSync('userProfileData')
            });
        }
        if (wx.getUserProfile) {
            this.setData({
                canIUseGetUserProfile: true
            });
        }
    },
    getUserProfile(e) {
        // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
        // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
        wx.getUserProfile({
            desc: app.globalData.isZh ? '用于完善会员资料' : 'To get your Avatar', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                // console.log(res)
                this.setData({
                    userProfileData: res
                });
                wx.setStorageSync('userProfileData', res.userInfo);
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                });
            },
            fail: (err) => {
                console.error(err)
            }
        });
    },
    getUserInfo(e) {
        // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        });
    },
    unbind() {
        wx.showModal({
            title: app.globalData.isZh ? '提示' : 'Wraning',
            content: app.globalData.isZh ? '是否确认解绑设备？' : "Are you sure?",
            confirmText: app.globalData.isZh ? '确认' : "Yes",
            cancelText: app.globalData.isZh ? '取消' : "No",
            success(res) {
                if (res.confirm) {
                    wx.closeBLEConnection({
                        deviceId: wx.getStorageSync('deviceId'),
                        success: (res) => {
                            // console.log('蓝牙断开成功', res) 
                        },
                        fail: (res) => {
                            console.error('蓝牙断开失败', res)
                        }
                    });
                    wx.closeBluetoothAdapter();
                    wx.showToast({ title: app.globalData.isZh ? "已解绑" : "Done", icon: "success", mask: true });
                    wx.removeStorageSync('deviceId');
                    setTimeout(wx.redirectTo, 1000, { url: '../connection/connection' });
                }
            }
        });
    },
    goto(e) {
        wx.navigateTo({ url: e.currentTarget.dataset.target });
    },
    logout() {
        wx.removeStorageSync('passWord');
        wx.removeStorageSync('userName');
        wx.removeStorageSync('Authorization');
        wx.redirectTo({ url: '../index/index' });
    }
})